package start;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Chapter07ATM {
    static Scanner input = new Scanner(System.in);
    static String[][] users = {
            {"aaaaa", "11111", "5000"},
            {"bbbbb", "22222", "8000"},
            {"ccccc", "33333", "6000"},
            {"ddddd", "44444", "7000"},
            {"eeeee", "55555", "10000"}
    };
    public  static int index = 0;

    //控制流程的方法
    public static void process() {
        System.out.println("~~~~~~~~~~欢迎使用黄崽银行~~~~~~~~~~");
        while (true){
            login();

        }


    }

    //登录 三次机会
    public static void login() {
        for(int j = 0 ; j < 3 ; j ++) {
            System.out.println("请输入账户");
            String account = input.next();
            System.out.println("请输入密码");
            String pwd = input.next();
            for (int i = 0; i < users.length; i++) {
                if (users[i][0].equals(account) && users[i][1].equals(pwd)) {
                    System.out.println("~~~~登录成功了~~~~");
                    index = i;
                    menu();
                    return;
                }
            }
            System.out.println("账户密码机会请重新输入,还剩" + (2 - j ) +"次机会");
        }
        System.out.println("？？？ 输入次数已用完，请联系银行取卡");
        System.exit(0);
    }

    //菜单
    public static void menu() {
        System.out.println("选择你要办理的业务");
        while (true) {
            System.out.println("1.存款  2.取钱  3.查询余额  4.转账 5.切换转户 6.更改密码 7.退出");
            int a = 0;
            try {
                a = input.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入有误请重写输入");
                input.next();
                continue;
            }
            switch (a) {
                case 1:
                    save();
                    break;
                case 2:
                    get();
                    break;
                case 3:
                    search();
                    break;
                case 4:
                    transfer();
                    break;
                case 5:
                    login();
                    break;
                case 6:
                    amend();
                    break;
                case 7:
                    System.out.println("感谢使用再见");
                    System.exit(0);


            }

        }
    }
    public static void smallMenu(){
        while (true) {
            System.out.println("1.返回菜单   2. 切换账户  3.退出银行");
            int a = 0;
            try {
                a = input.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入选项异常");
                input.next();
                continue;
            }
            if (a == 1) {
                menu();
            }
            if (a == 2) {
                login();
            }
            if (a == 3) {
                System.out.println("~~~~~~~~~~~感谢使用黄崽银行正在退出~~~~~~~~~~~");
                System.exit(0);
            } else {
                System.out.println("输入错误");
            }

        }

    }

    //存款
    public static void save() {
        while (true) {
            System.out.println("请输入你要存的金额");
            double deposit = 0.0;
            try {
                deposit = input.nextDouble();
            } catch (InputMismatchException e) {
                System.out.println("输入金额类型错误");
                input.next();
                continue;
            }
            users[index][2] = Double.parseDouble(users[index][2]) + deposit + "";
            System.out.println("您存入了" + deposit + "元");
            System.out.println("你的余额为" + users[index][2]);
            break;

        }
    }

    //取款
    public static void get() {
        while (true) {
            System.out.println("请输入你要取的余额");
            double balance = 0.0;
            try {
                balance = input.nextDouble();
            } catch (InputMismatchException e) {
                System.out.println("输入金额类型有误");
                input.next();
                continue;
            }
            if (Double.parseDouble(users[index][2]) > balance) {
                users[index][2] = Double.parseDouble(users[index][2]) - balance + "";
                System.out.println("你取走了" + balance + "元");
                smallMenu();
            } else {
                System.out.println("想想你有那么多钱吗");
                smallMenu();
            }
        }

    }

    //查询
    public static void search() {
        while (true) {
            System.out.println("你的余额为" + users[index][2]);
            break;
        }
    }

    //转账
    public static void transfer() {
        while (true) {
            System.out.println("请输入你要转账的账户");
            String account = input.next();
            for (int i = 0; i < users.length; i++) {
                if (users[i][0].equals(account)) {
                    System.out.println("崽种输入要转的金额");
                    double money = 0;
                    try {
                         money = input.nextDouble();
                    }catch (InputMismatchException e){
                      System.out.println("输入金额类型错误");
                      input.next();
                      continue;
                    }

                    if (money > Double.parseDouble(users[index][2])) {
                        System.out.println("你的余额不足");
                        smallMenu();
                    } else {
                        users[i][2] = Double.parseDouble(users[i][2]) + money + "";
                        users[index][2] = Double.parseDouble(users[index][2]) - money + "";
                        System.out.println("你向" + users[i][0] + "转了" + money + "元");
                        smallMenu();
                    }
                }
            }
            System.out.println("你输入的转户不正确请重新输入？");
            transfer();

        }
    }
    public static void amend() {
        while (true) {
            System.out.println("请输入你的旧密码");
            String password = "";
            try {
                password = input.next();
            }catch (InputMismatchException e){
                System.out.println("输入类型错误");
                input.next();
                continue;
            }

            if (users[index][1].equals(password)) {
                System.out.println("请再次输入旧密码");
                String password1 = "";
                try {
                    password1 = input.next();
                }catch(InputMismatchException e){
                    System.out.println("输入错误");
                    input.next();
                    continue;
                }
                if (users[index][1].equals(password1)) {
                    System.out.println("请输入新密码");
                    String password2 = "";
                    try {
                        password2 = input.next();
                    }catch (InputMismatchException e){
                        System.out.println("输入错误");
                        input.next();
                        continue;

                    }

                    users[index][1] = password2;
                    System.out.println("修改成功  请重新登录");
                    process();

                } else {
                    System.out.println("密码不正确");
                    smallMenu();
                }
            } else {
                System.out.println("密码不正确");
                smallMenu();
            }

        }
    }
    public static void amend1(){

    }


    public static void main(String[] args) {
        process();
    }
}
